/**
 *Copyright 2012-2013.All Rights Reserved
 */
package com.ketayao.dao.jdbc;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.SqlParameter;

/**
 * 
 * @author <a href="mailto:lazy_p@163.com">lazyp</a>
 * @version 2013-10-14
 */
public class JdbcTemplateProxy extends JdbcTemplate {
    private final Log    logger = LogFactory.getLog(getClass());
    private DataSource   slaveDataSource;
    private JdbcTemplate slaveJdbcTemplate;

    @Override
    public <T> T query(PreparedStatementCreator psc, PreparedStatementSetter pss, ResultSetExtractor<T> rse)
                                                                                                            throws DataAccessException {
        return this.getSlaveJdbcTemplate().query(psc, pss, rse);
    }

    @Override
    public <T> T query(String sql, ResultSetExtractor<T> rse) throws DataAccessException {
        return this.getSlaveJdbcTemplate().query(sql, rse);
    }

    @Override
    public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters)
                                                                                                        throws DataAccessException {
        return this.getSlaveJdbcTemplate().call(csc, declaredParameters);
    }

    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();
        logger.info("create proxy slave jdbctemplate");
        slaveJdbcTemplate = new JdbcTemplate(slaveDataSource);
    }

    public DataSource getSlaveDataSource() {
        return slaveDataSource;
    }

    public void setSlaveDataSource(DataSource slaveDataSource) {
        this.slaveDataSource = slaveDataSource;
    }

    public JdbcTemplate getSlaveJdbcTemplate() {
        return slaveJdbcTemplate;
    }

    public void setSlaveJdbcTemplate(JdbcTemplate slaveJdbcTemplate) {
        this.slaveJdbcTemplate = slaveJdbcTemplate;
    }

}
